﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyX.Core;
using MyX.DSL;
using LearningEnglish.Models;
using LearningEnglish.ViewModels;

namespace LearningEnglish.LogicDSL
{
    /// <summary>
    /// 详细说明了在不同设置下的票据的行为表现
    /// </summary>
    public class TokenLogicDSL : XIWorkDSL
    {
        public void Init()
        {
            var work = new WorkHelper();

            work.Event<TokenCreated>(
                e =>
                {
                    //默认单个渠道独占
                    VM.Tokens.Add(new TokenRD(e.XSenderID, e.EntityID, Token.Channel.APP, true, e.LiveTime));
                    VM.Tokens.Add(new TokenRD(e.XSenderID, e.EntityID, Token.Channel.Client, true, e.LiveTime));
                    VM.Tokens.Add(new TokenRD(e.XSenderID, e.EntityID, Token.Channel.Unknow, true, e.LiveTime));
                    VM.Tokens.Add(new TokenRD(e.XSenderID, e.EntityID, Token.Channel.Web, true, e.LiveTime));

                    //默认全局非独占
                    TokenRD.GlobalConfig.Add(e.XSenderID, e.IsExclusive);

                }, true);

            //设置全局的独占属性以及票据存活时间
            work.Event<TokenGlobalChanged>(
                e =>
                {
                    var tokenRDs = (from p in VM.Tokens where p.ID == e.XSenderID select p).ToList();
                    tokenRDs.ForEach(x =>
                    {
                        x.LiveTime = e.LiveTime;
                    });
                    TokenRD.GlobalConfig[e.XSenderID] = e.IsExclusive;

                }, true);

            //设置单独渠道的独占属性以及票据存活时间
            work.Event<TokenChannelChanged>(
                e =>
                {
                    var tokenRD = (from p in VM.Tokens where p.Channel == e.Channel select p).Single();
                    tokenRD.IsExclusive = e.IsExclusive;
                    tokenRD.LiveTime = e.LiveTime;

                }, true);

            work.Event<TokenLogined>(
                e =>
                {
                    //当全局独占时，则只有该渠道登录有效，其余渠道票据强制作废
                    var gloablExclusive = TokenRD.GlobalConfig[e.XSenderID];
                    if (gloablExclusive)
                    {
                        var otherTokenRDs = (from p in VM.Tokens where p.Channel != e.Channel && p.ID == e.XSenderID select p).ToList();
                        otherTokenRDs.ForEach(x => x.ForceRefresh());
                    }
                    var tokenRD = (from p in VM.Tokens where p.Channel == e.Channel && p.ID == e.XSenderID select p).Single();

                    tokenRD.AddTicket(e.TicketValue, e.TimeStamp);
                }, true);
            work.Event<TokenLogouted>(
                e =>
                {
                    //当全局独占时，则所有渠道票据强制作废
                    var gloablExclusive = TokenRD.GlobalConfig[e.XSenderID];
                    if (gloablExclusive)
                    {
                        var allTokenRDs = (from p in VM.Tokens where p.Channel == e.Channel && p.ID == e.XSenderID select p).ToList();
                        allTokenRDs.ForEach(x => x.ForceRefresh());
                    }
                    else
                    {
                        var tokenRD = (from p in VM.Tokens where p.Channel == e.Channel && p.ID == e.XSenderID select p).Single();
                        tokenRD.Refresh(e.TicketValue);
                    }
                }, true);
        }
    }
}